package com.example.groupmeal.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.example.groupmeal.config.repeatCommit.AvoidRepeatableCommit;
import com.example.groupmeal.config.webConfig.ApiBase;
import com.example.groupmeal.entity.TmsBanner;
import com.example.groupmeal.service.BannerService;
import com.example.groupmeal.utils.Result;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * 首页Controller类
 * @author ys
 *2020/01/30
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/api/banner", produces = "application/json;charset=UTF-8")
public class TmsBannerApi extends ApiBase<Object> {
    /** 日志处理的导入 */
    private static final Log logger = LogFactory.getLog(TmsBannerApi.class);

    @Autowired
    private BannerService bannerService;
    
    /**
     * 员工版首页banner
     * @param response
     * @return
     */
    @RequestMapping(value="/getBanner", method = RequestMethod.POST)
    public Result<?> getBanner(HttpServletResponse response) {
    	logger.info("员工版首页banner信息查询");
        try{
        	List<TmsBanner> bannerList = bannerService.getBannerInfo((byte)1);
        	return success(bannerList);
        }catch (Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }
    
    /**
     * 客户版首页banner
     * @param response
     * @return
     */
    @RequestMapping(value="/getBannerList", method = RequestMethod.POST)
    public Result<?> getBannerList(HttpServletResponse response) {
    	logger.info("客户版首页banner信息查询");
        try{
        	List<TmsBanner> bannerList = bannerService.getBannerInfo((byte)2);
        	return success(bannerList);
        }catch (Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 测试重复提交拦截
     * @param response
     * @return
     */
    @RequestMapping(value="/addBanner", method = RequestMethod.POST)
    @AvoidRepeatableCommit(timeout = 30000)
    public Result<?> addBanner(HttpServletResponse response) {
        logger.info("首页banner信息添加测试 -- 测试重复提交拦截");
        try{
            bannerService.addBannerInfo();
            return success(true);
        }catch (Exception e){
            e.printStackTrace();
            return error(e.getMessage());
        }
    }
    
}
